import { Controller,UseGuards } from '@nestjs/common';
import { Crud, CrudController, Override, ParsedRequest, CrudRequest, ParsedBody, CrudAuth } from '@nestjsx/crud';
import { ApiTags, ApiBody, ApiBearerAuth} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';
import { Collect } from '../../entities/collect.entity';
import { CollectService } from '../../services/collect/collect.service';

@Crud({
  model:{
    type:Collect,
  },
  query:{
    exclude:["user_id"],
    join:{
      product:{
        exclude:[]
      },
      'product.goods':{
        exclude:["category_id","brand_id","keywords","is_on_sale","is_delete","attribute_category","is_new","app_exclusive_price","is_app_exclusive","is_limited","is_hot","create_user_id","update_user_id","create_user_dept_id","promotion_tag","add_time","update_time","counter_price","extra_price","unit_price"]
      }
    }
  },
  routes:{
    only:['createOneBase','deleteOneBase','getManyBase']
  }
})
@CrudAuth({
  property:"user",
  filter: (user) => ({
    user_id: user.userId
  }),
  persist:(user) => {
    return user;
  }
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('collect')
@Controller('api/collect')
export class CollectController implements CrudController<Collect>{
  constructor(
    public service:CollectService
  ){}

  get base(): CrudController<Collect> {
    return this;
  }

  @ApiBody({
    type: Collect
  })
  @Override()
  createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Collect): Promise<Collect> {
    dto.user_id=req.parsed.authPersist.userId;
    dto.add_time=moment().unix();
    return this.base.createOneBase(req, dto);
  }

  @ApiBody({
    type: Collect
  })
  @Override()
  updateOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Collect): Promise<Collect> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.updateOneBase(req, dto);
  }
}
